﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.Model;
using rock.common;

namespace rock.UI.houseinfo
{
    public partial class UserControlHouseOwner : UserControl
    {
        DalHOUSE_OWNER dal = new DalHOUSE_OWNER();
        public UserControlHouseOwner()
        {
            InitializeComponent();
            this.userControlCommonListHeader1.addClick = addClick;
            this.userControlCommonListHeader1.updateClick = updateClick;
            this.userControlCommonListHeader1.deleteClick = deleteClick;
            this.userControlCommonListHeader1.searchClick = searchClick;
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            //this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
        }

      
       

        private void addClick()
        {
            FormHouseOwnerUpdate formUau = new FormHouseOwnerUpdate();
            formUau.oneRefreshParentData = refreshData;
            formUau.ShowDialog(this);
        }
        private void updateClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                FormHouseOwnerUpdate formUau = new FormHouseOwnerUpdate();

                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                ModelHOUSE_OWNER model = dal.GetModel(Convert.ToInt32(seqid));


                formUau._ModelHOUSE_OWNER = model;
             
                formUau.oneRefreshParentData = refreshData;
                formUau.ShowDialog(this);
            }
            else
            {
                MessageBox.Show("请选择要更新的业主.", "提示");
            }
        }
        private void deleteClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();


                DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dialogResult.Equals(DialogResult.OK))
                {
                    dal.Delete(Convert.ToInt32(seqid));
                   
                    //刷新数据
                    refreshData();
                }

            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }

         
        private void searchClick()
        {
            String search = this.userControlCommonListHeader1.textBox1.Text;
            refreshData();
        }

        private void refreshData() {

            String search = this.userControlCommonListHeader1.textBox1.Text;
            DataSet ds = dal.GetList("NAME LIKE  '%" + search + "%'");
            this.dataGridView1.DataSource = ds.Tables[0];
        }
        /// <summary>
        /// 双击更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            updateClick();
        }

        private void UserControlHouseOwner_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }
        
    }
}
